﻿using DataModels.Dto;
using DataModels.Dto.Livelihood;
using DataModels.Entities.Livelihood;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static DataModels.Enums.Livelihood.AppointmentListEnum;

namespace Services.Decoration
{
    public class DecorationRenovationService(SqlSugarClient db) : BaseService(db)
    {
        /// <summary>
        /// 获取案例类型列表
        /// </summary>
        /// <returns></returns>
        public ResponseModel GetCaseTypelist()
        {
            var list = db.Queryable<CaseType>().Select(n => new { UUID = n.UUID, Value = n.Type }).ToList();
            return Success(list);
        }

        /// <summary>
        /// 获得活动
        /// </summary>
        /// <param name="pageRequst"></param>
        /// <returns></returns>
        public ResponseModel GetDecorationRenovationPage(GetDecorationRenovationPageDot dot)
        {
            var total = 0;
            var res = db.Queryable<DecorationRenovation>()
                .LeftJoin<CaseType>((d, c) => d.CasetypeUUID == c.UUID)
                .WhereIF(dot.CasetypeUUID != null, (d, c) => d.CasetypeUUID == dot.CasetypeUUID)
                .OrderByDescending((d, c) => d.CreateTime)
                .Select((d, c) => new
                {
                    d.UUID,
                    d.Title,
                    c.Type,
                    d.HouseType,
                    d.Area,
                    d.Style,
                    d.DecorationLocation,
                    d.IsHandpick,
                    d.Stylist,
                    d.StylistNumber,
                    d.ImagePathS,
                    d.FilePath,
                    d.CreateTime
                }).ToPageList(dot.PageNumber, dot.PageSize, ref total);
            //重新映射
            var list = res.Select(n => new
            {
                n.UUID,
                n.Title,
                n.Type,
                n.HouseType,
                n.Area,
                n.Style,
                n.DecorationLocation,
                n.IsHandpick,
                n.Stylist,
                n.StylistNumber,
                ImagePathS = !string.IsNullOrEmpty(n.ImagePathS)
            ? n.ImagePathS.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList() //切割数据
            : new List<string>(),
                FilePath= !string.IsNullOrEmpty(n.FilePath)
            ? n.FilePath.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList() //切割数据
            : new List<string>(),
                n.CreateTime,
            });
            // 关键修改：分割为列表
            return PageSuccess(dot.PageNumber, dot.PageSize, total, list);
        }


        /// <summary>
        /// 获取意向类型列表
        /// </summary>
        /// <returns></returns>
        public ResponseModel GetIntentionTypelist()
        {
            var list = db.Queryable<IntentionType>().Select(n => new { UUID = n.UUID, n.ParentType ,Value = n.Type }).ToList();
            return Success(list);
        }
        /// <summary>
        /// 添加沟通意向
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ResponseModel AddAppointment([FromBody] AddAppointmentRequest request)
        {

            if (string.IsNullOrWhiteSpace(request.Phone))
                return Error("联系电话不能为空");

            if (request.IntentiontypeUUIDs == null || !request.IntentiontypeUUIDs.Any())
                return Error("至少选择一个意向类型");

            try
            {
                var appointment = new AppointmentList
                {
                    UUID = Guid.NewGuid().ToString(),
                    CustomerUUID = UserUUID.ToString(),
                    Phone = request.Phone,
                    IntentiontypeUUIDS = string.Join(";", request.IntentiontypeUUIDs),
                    CreateTime = DateTime.Now,
                    LatelyTime = DateTime.Now,
                    CommunicationState = CommState.NotCommunicated
                };

                // 插入数据库
                var result = db.Insertable(appointment).ExecuteCommand();

                return result > 0
                    ? Success("预约添加成功")
                    : Error("预约添加失败");
            }
            catch (Exception ex)
            {
                return Error($"错误: {ex.Message}");
            }
        }

        
    }
}
